package com.hisan.freeride.common.utils.utils;

import android.os.Handler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 单利Handler
 * Created by admin on 2017/5/4.
 */

public class MyHandlerUtil {
    private static MyHandlerUtil instance;
    private static List<IHandler> task = new ArrayList<IHandler>();
    private static Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            for (int i = 0; i < task.size(); i++) {
                try {
                    task.get(i).refeshData(msg);
                    task.remove(i);
                    i--;
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
    };

    public static synchronized MyHandlerUtil getInstance(IHandler iHandler) {
        if(instance == null){
            instance = new MyHandlerUtil();
        }
        if (!task.contains(iHandler)) {
            task.add(iHandler);
        }
        return instance;
    }

    public static Handler getHandler() {
        return handler;
    }

    public  Handler getHandler2() {
        return handler;
    }


    public interface IHandler {
        void refeshData(android.os.Message msg) throws IOException;
    }
}
